package cn.hnucm.orderservice.service.impl;

import cn.hnucm.orderservice.entity.TbCapital;
import cn.hnucm.orderservice.mapper.TbCapitalMapper;
import cn.hnucm.orderservice.service.TbCapitalService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 账单明细 服务实现类
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-06
 */
@Service
public class TbCapitalServiceImpl extends ServiceImpl<TbCapitalMapper, TbCapital> implements TbCapitalService {

    @Autowired
    private TbCapitalMapper tbCapitalMapper;


    /**
     * 输入一个用户id，获取其所有的支出账单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbCapital> getUserFromCapital(Integer userId) {
        try {
            return tbCapitalMapper.selectList(new QueryWrapper<TbCapital>()
                    .eq("fromUserId", userId)
                    .orderByAsc("transferAccountsTime"));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 输入一个用户的id，获取其所有的收入账单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbCapital> getUserToCapital(Integer userId) {
        try {
            return tbCapitalMapper.selectList(new QueryWrapper<TbCapital>()
                    .eq("toUserId", userId)
                    .orderByAsc("transferAccountsTime"));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 输入一个用户Id获取其所有的账单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbCapital> getUserAllCapital(Integer userId) {
        try {
            return tbCapitalMapper.selectList(new QueryWrapper<TbCapital>().eq("fromUserId", userId).or()
                    .eq("toUserId", userId)
                    .orderByAsc("transferAccountsTime"));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取某个用户一段事件内的所有账单
     * @param userId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Transactional
    @Override
    public List<TbCapital> getUserSometiomeCapital(Integer userId, String beginTime, String endTime) {

        try {
            return tbCapitalMapper.selectList(new QueryWrapper<TbCapital>()
                    .between("transferAccountsTime", beginTime, endTime)
                    .orderByAsc("transferAccountsTime"));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过账单号查询账单
     * @param capitalId
     * @return
     */
    @Transactional
    @Override
    public TbCapital getUserOneCapital(Integer capitalId) {
        try {
            return tbCapitalMapper.selectById(capitalId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 输入一个账单，插入一个账单
     * @param tbCapital
     * @return
     */
    @Transactional
    @Override
    public boolean addCapital(TbCapital tbCapital) {
        return tbCapitalMapper.insert(tbCapital) > 0;
    }


    /**
     * 输入一个订单号，删除其记录
     * @param capitalId
     * @return
     */
    @Transactional
    @Override
    public boolean deleteCapital(Integer capitalId) {
        return tbCapitalMapper.deleteById(capitalId)>0;
    }



}
